package com.itheima.service.impl;

import com.itheima.mapper.AccountMapper;
import com.itheima.pojo.Log;
import com.itheima.service.AccountService;
import com.itheima.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author ghy
 * @version 1.0
 * @date 2022-10-18 16:53
 */
@Service
@Transactional
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private LogService logService;

    /**
     * 转账
     *
     * @param outName 转账人
     * @param inName  入账人
     * @param money   转账金额
     * @return
     */
    @Override
    @Transactional    //以这个为准
    public boolean transfer(String outName, String inName, Double money) {
        try {
            accountMapper.outMoney(outName, money);   //转出去
        //int i = 1/0;
            accountMapper.inMoney(inName,money);   //转进来
        } finally {
            Log log = new Log();
            log.setCreateDate(new Date());
            log.setInfo(outName + "向" + inName + "转账" + money + "￥");
            logService.save(log);
        }
        return true;
    }
}
